/*
 * @Author: fhw
 * @Date: 2022-08-02 14:39:32
 * @Description:
 */
import { Body, Controller, Get, HttpCode, Post, Query, UseGuards, Request, Param } from '@nestjs/common'
import { ApiResponse, ApiOperation, ApiTags, ApiBody, ApiQuery, ApiBearerAuth, ApiParam } from '@nestjs/swagger'
import { ChatUserService } from './chat_user.service'
import { GetUserInfoDto, GetUserPageDto, RegisterChatUserDto, UpdateChatUserDto, UpdatePassWordDto, ValidateUserDto } from './dto/chat_user.dto'
import { ChatUser, ChatUserResult } from './entities/chat_user.entity'
import { AuthGuard } from '@nestjs/passport';
import { throwHttp } from 'src/utlis'
import { DelteIdDto } from 'src/common/dto/dto'
@ApiBearerAuth() // swagger文档设置token
@Controller('chat-user')
@ApiTags('用户')
export class ChatUserController {
  constructor(private readonly chatUserService: ChatUserService) { }
  @HttpCode(200)
  @Get('/getUserPage')
  @ApiResponse({
    status: 200,
    description: '获取用户列表',
    type: ChatUserResult,
  })
  @ApiOperation({ summary: '获取用户列表', operationId: 'getUserPage' })

  @UseGuards(AuthGuard('jwt'))//jwt策略，身份鉴权
  async getUserPage(@Query() params: GetUserPageDto) {
    return this.chatUserService.getUserPage(params)
  }

  @HttpCode(200)
  @Get('/getUserInfo')
  @ApiResponse({
    status: 200,
    description: '获取用户信息',
    type: ChatUserResult,
  })
  @ApiOperation({ summary: '获取用户信息', operationId: 'getUserInfo' })
  @UseGuards(AuthGuard('jwt'))//jwt策略，身份鉴权
  async getUserInfo(@Query() params: GetUserInfoDto) {
    try {
      return await this.chatUserService.getUserInfo(params)
    } catch (error) {
      console.log(error.message);
      throwHttp(error.message)
    }
  }

  @HttpCode(200)
  @Post('/registerChatUser')
  @ApiQuery({
    description: '聊天用户注册',
    type: RegisterChatUserDto,
  })
  @ApiBody({ description: '聊天用户注册', type: RegisterChatUserDto })
  @ApiOperation({ summary: '聊天用户注册', operationId: 'registerChatUser' })
  @ApiResponse({ status: 200, type: ChatUser })
  registerChatUser(@Body() body: RegisterChatUserDto) {
    return this.chatUserService.registerChatUser(body)
  }

  @HttpCode(200)
  @Post('/updateChatUser')
  @ApiQuery({
    description: '更新用户资料',
    type: UpdateChatUserDto,
  })
  @ApiBody({ description: '更新用户资料', type: UpdateChatUserDto })
  @ApiOperation({ summary: '更新用户资料', operationId: 'updateChatUser' })
  @UseGuards(AuthGuard('jwt'))//jwt策略，身份鉴权
  updateChatUser(@Body() body: UpdateChatUserDto) {
    return this.chatUserService.updateChatUser(body)
  }
  @HttpCode(200)
  @Post('/login')
  @ApiQuery({
    description: '登录',
    type: ValidateUserDto,
  })
  @ApiBody({ description: '登录', type: ValidateUserDto })
  @ApiOperation({ summary: '登录', operationId: 'login' })
  login(@Body() body: ValidateUserDto) {
    try {
      return this.chatUserService.login(body)
    } catch (error) {
      throwHttp(error.message)
    }
  }
  @HttpCode(200)
  @Get('userInfo')
  @UseGuards(AuthGuard('jwt'))//jwt策略，身份鉴权
  getUser(@Request() req) {
    return req.user;
  }

  @HttpCode(200)
  @Post('/delChatUser')
  @ApiQuery({
    description: '删除用户',
    type: DelteIdDto,
  })
  @ApiBody({ description: '删除用户', type: DelteIdDto })
  @ApiOperation({ summary: '删除用户', operationId: 'delChatUser' })
  @UseGuards(AuthGuard('jwt'))//jwt策略，身份鉴权
  delChatUser(@Body() body: DelteIdDto) {
    return this.chatUserService.delChatUser(body);
  }
  @HttpCode(200)
  @Get('/getContacts/:userId')
  @ApiParam({
    name: 'userId',
    description: '用户ID',
    type: String,
  })
  @UseGuards(AuthGuard('jwt'))//jwt策略，身份鉴权
  @ApiOperation({ summary: '获取用户联系人与群聊', operationId: 'getContacts' })
  getContacts(@Param('userId') userId: string) {
    return this.chatUserService.getContacts(userId)
  }
  @HttpCode(200)
  @Get('/getAccountSecurity/:userId')
  @ApiParam({
    name: 'userId',
    description: '用户ID',
    type: String,
  })
  @UseGuards(AuthGuard('jwt'))//jwt策略，身份鉴权
  @ApiOperation({ summary: '获取用户账号安全性', operationId: 'getAccountSecurity' })
  getAccountSecurity(@Param('userId') userId: string) {
    return this.chatUserService.getAccountSecurity(Number(userId))
  }
  @HttpCode(200)
  @Post('/updatePassWord')
  @ApiQuery({
    description: '修改密码',
    type: UpdatePassWordDto,
  })
  @ApiBody({ description: '修改密码', type: UpdatePassWordDto })
  @ApiOperation({ summary: '修改密码', operationId: 'updatePassWord' })
  @UseGuards(AuthGuard('jwt'))//jwt策略，身份鉴权
  @ApiResponse({ status: 200, type: String })
  updatePassWord(@Body() body: UpdatePassWordDto) {
    return this.chatUserService.updatePassWord(body)
  }
}
